/*
 *  Copyright 2009 rohanranade.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.jqif;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Categories in the QIF file
 * 
 * @author Rohan Ranade
 */
public class CategoriesList {

    public static final String SECTION_HEADER = "!Type:Cat";

    public static final char NAME_FIELD = 'N';
    public static final char DESCRIPTION_FIELD = 'D';
    public static final char TAX_FIELD = 'T';
    public static final char INCOME_FIELD = 'I';
    public static final char EXPENSE_FIELD = 'E';
    public static final char BUDGET_FIELD = 'B';
    public static final char TAX_SCHEDULE_FIELD = 'R';

    private List<Category> categories = new ArrayList<Category>();

    public CategoriesList() {
    }

    public List<Category> getCategories() {
        return Collections.unmodifiableList(categories);
    }

    public void addCategory(Category category) {
        categories.add(category);
    }

    public void removeCategory(Category category) {
        categories.remove(category);
    }

    // TODO Currently category parent child links are not created
    public void load(QIFSource source) throws IOException {
        categories.clear();
        String line;
        boolean done = false;
        Category cat = new Category();
        while (!done) {
            line = source.readLine();
            if (line == null) {
                done = true;
                continue;
            }

            char field = Character.toUpperCase(line.charAt(0));
            switch (field) {
                case NAME_FIELD:
                    cat.setName(line.substring(1));
                    break;
                case DESCRIPTION_FIELD:
                    cat.setDescription(line.substring(1));
                    break;
                case TAX_FIELD:
                    cat.setTaxRelated(true);
                    break;
                case INCOME_FIELD:
                    cat.setIncome(true);
                    break;
                case EXPENSE_FIELD:
                    cat.setIncome(false);
                    break;
                case BUDGET_FIELD:
                    // TODO currently unsupported.
                    break;
                case TAX_SCHEDULE_FIELD:
                    cat.setTaxScheduleInfo(line.substring(1));
                    break;
                case '^':
                    categories.add(cat);
                    cat = new Category();
                    line = source.peekLine();
                    if (line != null && line.startsWith("!")) {
                        done = true;
                    }
                    break;
            }
        }
    }
}
